package com.cloudm.technician.ui.base

import android.Manifest
import com.cloudm.technician.R
import pub.devrel.easypermissions.EasyPermissions
import pub.devrel.easypermissions.PermissionRequest

/**
 * 作者: wesley
 * 时间: 2018/1/4
 * 描述: 权限相关的fragment
 */
abstract class BasePermissionsFragment<T : BasePresenter> : BaseFileFragment<T>(),
        EasyPermissions.PermissionCallbacks {

    companion object {
        private const val REQUEST_PERMISSIONS_FOR_CAMEAR = 100
        private const val REQUEST_CODE_SELECT_FROM_ALBUM_PERMISSIONS = 101
        private const val REQUEST_CODE_SCAN_QR_CODE = 102
        private const val REQUEST_CODE_PERMISSIONS_FOR_MAP = 103
    }

    override fun onPermissionsGranted(requestCode: Int, perms: MutableList<String>) {
        when (requestCode) {
            REQUEST_PERMISSIONS_FOR_CAMEAR -> {
                if (perms.containsAll(listOf(Manifest.permission.WRITE_EXTERNAL_STORAGE,
                        Manifest.permission.CAMERA))) {
                    takePictureFromCamera()
                }
            }

            REQUEST_CODE_SELECT_FROM_ALBUM_PERMISSIONS -> {
                takePicFromGalley()
            }

            REQUEST_CODE_SCAN_QR_CODE -> {
                if (perms.containsAll(listOf(Manifest.permission.CAMERA,
                        Manifest.permission.WRITE_EXTERNAL_STORAGE))) {
                    scanRrCodeGrantedSuccess()
                }
            }

            REQUEST_CODE_PERMISSIONS_FOR_MAP -> {
                if (perms.containsAll(listOf(Manifest.permission.CAMERA,
                        Manifest.permission.ACCESS_COARSE_LOCATION,
                        Manifest.permission.ACCESS_FINE_LOCATION,
                        Manifest.permission.WRITE_EXTERNAL_STORAGE,
                        Manifest.permission.READ_PHONE_STATE))) {
                    locationGrantedSuccess()
                }
            }
        }
    }

    override fun onPermissionsDenied(requestCode: Int, perms: MutableList<String>) {
        if (EasyPermissions.somePermissionPermanentlyDenied(this, perms)) {
            showAppSettingDialog()
        }
    }

    override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<out String>,
                                            grantResults: IntArray) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults)
        EasyPermissions.onRequestPermissionsResult(requestCode, permissions, grantResults, this)
    }


    /**
     * 获取拍照的权限
     */
    protected fun requestPermissionsForCamera() {
        EasyPermissions.requestPermissions(
                PermissionRequest.Builder(this, REQUEST_PERMISSIONS_FOR_CAMEAR,
                        Manifest.permission.CAMERA,
                        Manifest.permission.WRITE_EXTERNAL_STORAGE).setRationale(
                        R.string.request_permission_by_camera).setPositiveButtonText(
                        R.string.ok).setNegativeButtonText(R.string.cancel).build())
    }

    /**
     * 获取从相册中选取的权限
     */
    protected fun requestPermissionFromGalley() {
        EasyPermissions.requestPermissions(
                PermissionRequest.Builder(this, REQUEST_CODE_SELECT_FROM_ALBUM_PERMISSIONS,
                        Manifest.permission.WRITE_EXTERNAL_STORAGE).setRationale(
                        R.string.request_permission_by_select_from_album).setPositiveButtonText(
                        R.string.ok).setNegativeButtonText(R.string.cancel).build())
    }


    /**
     * 获取扫描二维码的权限
     */
    protected fun requestPermissionsForScanQrCode() {
        EasyPermissions.requestPermissions(
                PermissionRequest.Builder(this, REQUEST_CODE_SCAN_QR_CODE,
                        Manifest.permission.CAMERA,
                        Manifest.permission.WRITE_EXTERNAL_STORAGE).setRationale(
                        R.string.qr_code_need_camera_permissions).setPositiveButtonText(
                        R.string.ok).setNegativeButtonText(R.string.cancel).build())
    }


    /**
     * 获取地图的权限
     */
    protected fun requestPermissionsForMap() {
        EasyPermissions.requestPermissions(
                PermissionRequest.Builder(this, REQUEST_CODE_PERMISSIONS_FOR_MAP,
                        Manifest.permission.CAMERA, Manifest.permission.ACCESS_COARSE_LOCATION,
                        Manifest.permission.ACCESS_FINE_LOCATION,
                        Manifest.permission.WRITE_EXTERNAL_STORAGE,
                        Manifest.permission.READ_PHONE_STATE).setRationale(
                        R.string.request_location_permissions).setPositiveButtonText(
                        R.string.ok).setNegativeButtonText(R.string.cancel).build())
    }


    /**
     * 扫描二维码授权成功
     */
    protected open fun scanRrCodeGrantedSuccess() {

    }


    /**
     * 地图授权成功
     */
    protected open fun locationGrantedSuccess() {

    }
}